/**
 * Copyright (C) 2011 Andrew C. Love (DNC) <dnc.app.sup@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dnc.cloak.framework.util.datacompare;

import java.util.Date;

import com.dnc.cloak.framework.spring.SpringIntegration;
import com.dnc.cloak.framework.util.dataconvert.DataConversionException;
import com.dnc.cloak.framework.util.dataconvert.DataConverter;
import com.dnc.cloak.service.ConfigurationService;

public class DataComparator {
    
    static public boolean evaluate(Class<?> dataClass, Object data1, String operator, Object data2) throws DataCompareException {
    	
        try {
			if (dataClass == String.class) {        	
				String convert1 = DataConverter.getString(data1); 
				String convert2 = DataConverter.getString(data2);
				
				if (operator.equalsIgnoreCase("=")){
					return (convert1.equalsIgnoreCase(convert2)) ? true : false;
				} 
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.equalsIgnoreCase(convert2)) ? true : false;
				}
				else if (operator.equalsIgnoreCase("*=")){
					return (convert1.toLowerCase().indexOf(convert2.toLowerCase()) >= 0) ? true : false;
				} 
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}  
			}
			
			else if (dataClass == Integer.class) {
				Integer convert1 = DataConverter.getInteger(data1); 
				Integer convert2 = DataConverter.getInteger(data2); 
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			
			else if (dataClass == Boolean.class) {
			   	Boolean convert1 = DataConverter.getBoolean(data1); 
			   	Boolean convert2 = DataConverter.getBoolean(data2); 

				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
			}
			else if (dataClass == Long.class) {
			   	Long convert1 = DataConverter.getLong(data1); 
			   	Long convert2 = DataConverter.getLong(data2);

				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
			   	
			}
			else if (dataClass == Short.class) {
			   	Short convert1 = DataConverter.getShort(data1); 
			   	Short convert2 = DataConverter.getShort(data2);
			   	
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
			   	
			}
			else if (dataClass == Double.class) {
				Double convert1 = DataConverter.getDouble(data1); 
				Double convert2 = DataConverter.getDouble(data2); 
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			else if (dataClass == Date.class) {
			    String dateFormat = ((ConfigurationService)SpringIntegration.context.getBean("configurationService")).getProperty(null, "dateformat", "yyyy-MM-dd");        

				Date convert1 = (data1 instanceof String) ? DataConverter.getDate((String)data1,dateFormat) : DataConverter.getDate (data1);
				Date convert2 = (data2 instanceof String) ? DataConverter.getDate((String)data2,dateFormat) : DataConverter.getDate (data2);
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			else if (dataClass == Float.class) {
				Float convert1 = DataConverter.getFloat(data1); 
				Float convert2 = DataConverter.getFloat(data2);
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			else if (dataClass == Number.class) {
				Integer convert1 = DataConverter.getInteger(data1); 
				Integer convert2 = DataConverter.getInteger(data2);
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			else if (dataClass == Byte.class) {
				Byte convert1 = DataConverter.getByte(data1); 
				Byte convert2 = DataConverter.getByte(data2);
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			else if (dataClass == Character.class) {
				Character convert1 = DataConverter.getCharacter(data1); 
				Character convert2 = DataConverter.getCharacter(data2);
				
				if (operator.equalsIgnoreCase(">")) {
					return (convert1.compareTo(convert2)>0) ? true : false;
				}
				else if (operator.equalsIgnoreCase(">=")){
					return (convert1.compareTo(convert2)>=0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<")){
					return (convert1.compareTo(convert2) < 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("<=")){
					return (convert1.compareTo(convert2) <= 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("!=")){
					return !(convert1.compareTo(convert2) == 0) ? true : false;
				}
				else if (operator.equalsIgnoreCase("=")){
					return (convert1.compareTo(convert2) == 0) ? true : false;
				} 
				
			}
			else {
				throw new DataCompareException("Unknown data type");
			}
		} 
        catch (DataConversionException ex) {
			throw new DataCompareException(ex);
		}
        
        return false;
        
    }
    
}